﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Pages_TimKiem : System.Web.UI.Page
{
    public Entities en = new Entities();
    public string strTen = string.Empty;
    public string strNhom = string.Empty;
    public string ss = string.Empty;
    private DataAccess da = new DataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
       // DropDownList1.Items.Clear();
        if (!IsPostBack)
        {
            this.DropDownList1.Items.Clear();
            this.getNhomSP();
        }

        strNhom = Request.QueryString["key"].ToString();
        strTen = Request.QueryString["key1"].ToString();
        if (strNhom == "") {
            this.getSPtheoten(strTen);  
        }
        if (strTen == "") {            
            this.getSPtheoNhom(strNhom);
            this.nhomsanpham.Text = strNhom;
        }
        if (strNhom == "" & strTen == "") {
            this.getAllSP();
        }
        
    }   
    private void getSPtheoten(string key)
    {
        DataSet ds = new DataSet();
        SqlDataAdapter adt = new SqlDataAdapter();
        SqlCommand comm = new SqlCommand("SeachHang", da.SqlConn());
        comm.Parameters.AddWithValue("@Nhom", ss);
        comm.Parameters.AddWithValue("@TenHang", key);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Connection.Open();
        adt.SelectCommand = comm;
        adt.Fill(ds);
        CollectionPager1.MaxPages = 100;
        CollectionPager1.PageSize = 4;
        CollectionPager1.DataSource = ds.Tables[0].DefaultView;
        CollectionPager1.BindToControl = DTL_SP;
        DTL_SP.DataSource = CollectionPager1.DataSourcePaged;
        DTL_SP.DataBind();
    }
    private void getSPtheoNhom(string key)
    {
        DataSet ds = new DataSet();
        SqlDataAdapter adt = new SqlDataAdapter();
        SqlCommand comm = new SqlCommand("SeachHang", da.SqlConn());
        comm.Parameters.AddWithValue("@Nhom", key);
        comm.Parameters.AddWithValue("@TenHang", ss);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Connection.Open();
        adt.SelectCommand = comm;
        adt.Fill(ds);
        CollectionPager1.MaxPages = 100;
        CollectionPager1.PageSize = 4;
        CollectionPager1.DataSource = ds.Tables[0].DefaultView;
        CollectionPager1.BindToControl = DTL_SP;
        DTL_SP.DataSource = CollectionPager1.DataSourcePaged;
        DTL_SP.DataBind();
    }
    private void getNhomSP() {
        SqlCommand comm1 = new SqlCommand("GetAllNhomHang", da.SqlConn());
        comm1.CommandType = CommandType.StoredProcedure;
        SqlDataReader drd1;
        comm1.Connection.Open();
        drd1 = comm1.ExecuteReader();
        while (drd1.Read())
        {
            ListItem ListNhom = new ListItem();
            ListNhom.Text = "" + drd1["TenNhomHang"].ToString();
            ListNhom.Value = drd1["MaNhomHang"].ToString();
            DropDownList1.Items.Add(ListNhom);
        }
        drd1.Close();
        comm1.Connection.Close();
        comm1.Connection.Dispose();
    }
    private void getAllSP()
    {
        DataSet ds = new DataSet();
        SqlDataAdapter adt = new SqlDataAdapter();
        SqlCommand comm = new SqlCommand("getHang", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@MaNhom", "");
        comm.Parameters.AddWithValue("@MaHang", "");
        comm.Parameters.AddWithValue("@Ten", "");
        comm.Connection.Open();
        adt.SelectCommand = comm;
        adt.Fill(ds);
        CollectionPager1.MaxPages = 100;
        CollectionPager1.PageSize = 4;
        CollectionPager1.DataSource = ds.Tables[0].DefaultView;
        CollectionPager1.BindToControl = DTL_SP;
        DTL_SP.DataSource = CollectionPager1.DataSourcePaged;
        DTL_SP.DataBind();
    }
    protected void LinkAddvaogiohang_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string strMaNhom = DropDownList1.SelectedItem.ToString();
        this.nhomsanpham.Text = strMaNhom;
        this.getSPtheoNhom(strMaNhom);
        
    }
}
